package a05_双指针法;

/**
 * <p>
 * a06_删除链表的倒数第N个节点复习4
 * </p>
 *
 * @author flyduck
 * @since 2025/2/19
 */
public class a06_删除链表的倒数第N个节点复习4 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        //a b c d e f 3
        //    slow
        //            fast

        ListNode dummyNode = new ListNode(0);
        dummyNode.next = head;

        ListNode slowNode = dummyNode;
        ListNode fastNode = dummyNode;
        while (n + 1 > 0) {
            fastNode = fastNode.next;
            n--;
        }

        while (fastNode != null){
            slowNode = slowNode.next;
            fastNode = fastNode.next;
        }

        slowNode.next = slowNode.next.next;

        return dummyNode.next;
    }
}
